<?php
session_start();

if (!isset($_SESSION['username'])) {
    header("Location: ../login.php");
    exit();
}

// 数据库配置
include 'config.php';

// 添加单个卡密
$kami = isset($_GET['kami']) ? $conn->real_escape_string($_GET['kami']) : null;
$type = isset($_GET['type']) ? $conn->real_escape_string($_GET['type']) : null;

if ($kami === null || $type === null) {
    echo json_encode(array("code" => "403", "error" => "参数不全"));
    exit();
}

// 检查数据库中是否已经存在相同的卡密
$checkSql = "SELECT * FROM kami WHERE kami = ?";
$stmt = $conn->prepare($checkSql);
$stmt->bind_param("s", $kami);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    echo json_encode(array("code" => "401", "error" => "卡密重复"));
    $stmt->close();
    exit();
}

// 如果不存在相同的卡密，执行插入操作
$sql = "INSERT INTO kami (kami, type, zt, vip_time, qq) VALUES (?, ?, 0, '', '')";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $kami, $type);
if ($stmt->execute()) {
    echo json_encode(array("code" => "200", "msg" => "卡密添加成功", "kami" => $kami));
} else {
    echo json_encode(array("code" => "404", "error" => $conn->error));
}
$stmt->close();
$conn->close();
?>
